﻿using Quartz;


namespace Nasty.QuartzModule
{
    public static class QuartzExtension
    {
        /// <summary>
        /// 注册AddNastyQuartz,在AddNastyHost之后调用
        /// </summary>
        /// <param name="services"></param>
        public static void AddNastyQuartz(this IServiceCollection services)
        {
            // 注册我们创建的动态任务服务
            services.AddScoped<QuartzDynamicService>();
            services.AddSingleton<QuartzJobListener>();

            // 注册 Quartz 服务
            services.AddQuartz(q =>
            {
                // 设置 misfire 阈值和其他配置
                q.Properties["quartz.jobStore.misfireThreshold"] = "5000";

                // 可选：使用自定义配置（如线程池大小）
                q.UseSimpleTypeLoader();
                q.UseInMemoryStore();
                q.UseDefaultThreadPool(tp => tp.MaxConcurrency = 10);
                // 注册监听器
                q.AddJobListener<QuartzJobListener>();
            });

            services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);  
        }
    }
}
